package com.shuai.stickynote;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.shuai.adapter.LeftMenuAdapter;
import com.shuai.adapter.MoreMenuAdapter;
import com.shuai.fragment.MemmoryFragemnt;
import com.shuai.fragment.PlantFragment;
import com.shuai.fragment.TaskFragment;
import com.shuai.fragment.TextFragment;
import com.shuai.sql.SQL;
import com.shuai.tool.ClassFactory;
import com.shuai.tool.MyTime;
import com.shuai.tool.SendTool;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class EditActivity extends AppCompatActivity {
    private static final String TAG = "fuck";
    private DrawerLayout drawerLayout;
    public static ListView menuList1,menuList2;
    private static ArrayList<LeftMenu> data1;
    private ArrayList<LeftMenu> data2;
    private LeftMenuAdapter adapter1,adapter2;
    private ImageView v_more;
    private Context context;
    private ArrayList<MoreMenuItem> moreMenuListData;
    private MoreMenuAdapter moreMenuAdapter;
    private ImageView menu_more;
    private static MemmoryFragemnt memmoryFragemnt;
    private static PlantFragment plantFragment;
    private static TaskFragment taskFragment;
    private static TextFragment textFragment;
    public static FragmentManager fManager;
    public static TextView tv_title;
    public static ImageView iv_delete, iv_theme;
    public static SQL sql;
    private String action = "";
    public static int widgetId = 0;
    public static String size = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        init();
        initFragment();
        findViewById(R.id.edit_menu_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });
        setLeftMenu();
        getInfo();
    }

    private void setLeftMenu(){
        data1 = new ArrayList<>();
        data2 = new ArrayList<>();
        data1.add(new LeftMenu(R.drawable.ic_text,"及时便签", false));
        data1.add(new LeftMenu(R.drawable.ic_plant,"日期计划", false));
        data1.add(new LeftMenu(R.drawable.ic_task,"目标任务", false));
        data1.add(new LeftMenu(R.drawable.ic_memory,"记忆模块", false));

        data2.add(new LeftMenu(R.drawable.ic_setting,"设置",false));
        data2.add(new LeftMenu(R.drawable.ic_delete,"主题",false));

        adapter1 = new LeftMenuAdapter(EditActivity.this,data1);
        adapter2 = new LeftMenuAdapter(EditActivity.this,data2);
        menuList1.setAdapter(adapter1);
        menuList2.setAdapter(adapter2);
        menuList1.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        menuList1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter1.setItemSelected(position);
                drawerLayout.closeDrawers();
                adapter1.notifyDataSetChanged();
                showFragment(position);
            }
        });
        menuList1.performItemClick(menuList1.getChildAt(0),0,menuList1.getItemIdAtPosition(0));
        menuList2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(context, "尚未开发！", Toast.LENGTH_SHORT).show();
                drawerLayout.closeDrawers();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("RestrictedApi")
    public void showPopupMenu(View view) {
//        popupMenu方案
//        PopupMenu popupMenu = new PopupMenu(EditActivity.this,view);
//        popupMenu.getMenuInflater().inflate(R.menu.edit_right_menu,popupMenu.getMenu());
//        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                switch (item.getItemId()){
//                    case R.id.right_color:
//                        Toast.makeText(EditActivity.this, "color", Toast.LENGTH_SHORT).show();
//                        break;
//                    case R.id.right_serch:
//                        Toast.makeText(EditActivity.this, "serch", Toast.LENGTH_SHORT).show();
//                        break;
//                }
//                return true;
//            }
//        });
//        setIconEnable(popupMenu.getMenu(),true);
//        popupMenu.show();
//        popupWindow方案
        View converView = LayoutInflater.from(context).inflate(R.layout.more_menu_popup,null,false);
        ListView menuList = converView.findViewById(R.id.more_menu_list);
        moreMenuListData = new ArrayList<>();
        moreMenuListData.add(new MoreMenuItem(R.drawable.ic_color,"测试"));
        moreMenuListData.add(new MoreMenuItem(R.drawable.ic_color,"测试"));
        moreMenuAdapter = new MoreMenuAdapter(context,moreMenuListData);
        menuList.setAdapter(moreMenuAdapter);
        final PopupWindow popupWindow = new PopupWindow(converView,400, ViewGroup.LayoutParams.WRAP_CONTENT,true);
        popupWindow.setTouchable(true);
        popupWindow.setOverlapAnchor(true);
        popupWindow.setFocusable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        popupWindow.showAsDropDown(view,-popupWindow.getWidth()+90,20);
        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(EditActivity.this, moreMenuListData.get(position).getName(), Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
    }

    private void setIconEnable(Menu menu, boolean enable){
        try{
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            m.setAccessible(true);
            m.invoke(menu, enable);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void init(){
        sql = new SQL(EditActivity.this);
        drawerLayout = findViewById(R.id.drawerLayout);
        menuList1 = findViewById(R.id.left_menu1List);
        menuList2 = findViewById(R.id.left_menu2List);
        tv_title = findViewById(R.id.edit_title);
        iv_delete = findViewById(R.id.edit_menu_delete);
        iv_theme = findViewById(R.id.edit_menu_theme);
        context = EditActivity.this;
        fManager = getSupportFragmentManager();
    }

    public static void hideAllFragment(FragmentTransaction fragmentTransaction){
        if (memmoryFragemnt != null)fragmentTransaction.hide(memmoryFragemnt);
        if (plantFragment != null)fragmentTransaction.hide(plantFragment);
        if (taskFragment != null)fragmentTransaction.hide(taskFragment);
        if (textFragment != null)fragmentTransaction.hide(textFragment);
//        if (textEditFragment != null)fragmentTransaction.hide(textEditFragment);
    }

    public static void showFragment(FragmentTransaction fTransaction, int i){
        iv_delete.setVisibility(View.GONE);
        iv_theme.setVisibility(View.GONE);
        switch (i){
            case 0:
                if (textFragment == null){
                    textFragment = new TextFragment();
                    fTransaction.add(R.id.edit_layout_content,textFragment);
                }else fTransaction.show(textFragment);
                tv_title.setText(data1.get(i).getName());
                break;
            case 1:
                if (plantFragment == null){
                    plantFragment = new PlantFragment();
                    fTransaction.add(R.id.edit_layout_content,plantFragment);
                }else fTransaction.show(plantFragment);
                if (PlantFragment.calendarView != null){
                    tv_title.setText(PlantFragment.getDate());
                }
                break;
            case 2:
                if (taskFragment == null){
                    taskFragment = new TaskFragment();
                    fTransaction.add(R.id.edit_layout_content,taskFragment);
                }else fTransaction.show(taskFragment);
                tv_title.setText(data1.get(i).getName());
                break;
            case 3:
                if (memmoryFragemnt == null){
                    memmoryFragemnt = new MemmoryFragemnt();
                    fTransaction.add(R.id.edit_layout_content,memmoryFragemnt);
                }else fTransaction.show(memmoryFragemnt);
                tv_title.setText(data1.get(i).getName());
                break;
        }
    }

    private void showFragment(int i){
        Fragment fragment = null;
        tv_title.setOnClickListener(null);
        switch (i){
            case 0:
                fragment = new TextFragment();
                tv_title.setText(data1.get(i).getName());
                break;
            case 1:
                fragment = new PlantFragment();
                break;
            case 2:
                fragment = new TaskFragment();
                tv_title.setText(data1.get(i).getName());
                break;
            case 3:
                fragment = new MemmoryFragemnt();
                tv_title.setText(data1.get(i).getName());
                break;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.edit_layout_content,fragment).commit();
    }

    public void clearContent(View view) {
//        if (TextEditFragment.et_content.getText().equals("")){
//            Toast.makeText(context, "已清空！", Toast.LENGTH_SHORT).show();
//        }else {
//            TextEditFragment.et_content.setText("");
//        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initFragment(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (textFragment == null)textFragment = new TextFragment();
                if (plantFragment == null)plantFragment = new PlantFragment();
                if (taskFragment == null)taskFragment = new TaskFragment();
                if (memmoryFragemnt == null)memmoryFragemnt = new MemmoryFragemnt();
            }
        }).start();
    }

    private void getInfo(){
        Intent intent = getIntent();
        action = intent.getAction();
        if (action.equals(SendTool.WIDGET_ACTION_TEXT) || action.equals(SendTool.WIDGET_ACTION_PLANT)
           || action.equals(SendTool.WIDGET_ACTION_TASK) || action.equals(SendTool.WIDGET_ACTION_MENORY)){
            widgetId = intent.getIntExtra("id",0);
            size = intent.getStringExtra(ClassFactory.SIZE);
        }
    }

}